package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("menu")
public class MenuController {
    @Reference
    private MenuService menuService;

    //新增菜单项
    @RequestMapping("add")
    public Result add(@RequestBody Menu menu) {
        try {
            menuService.add(menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_MENUITEM_FAIL);
        }
        return new Result(true, MessageConstant.ADD_MENUITEM_SUCCESS);
    }

    //分页查询
    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        //使用分页插件
        return menuService.findPage(queryPageBean);
    }

    //回显表单数据
    @RequestMapping("findById")
    public Result findById(Integer id) {
        try {
            Menu menu = menuService.findById(id);
            return new Result(true, MessageConstant.GET_MENU_SUCCESS, menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MENU_FAIL);
        }
    }

    //编辑
    @RequestMapping("edit")
    public Result edit(@RequestBody Menu menu) {
        try {
            menuService.edit(menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_MENU_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_MENU_SUCCESS);
    }

    //删除
    @RequestMapping("delete")
    public Result delete(Integer id) {
        try {
            menuService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_MEUNORROLE_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_MENU_SUCCESS);
    }

    //动态展示菜单栏
    @RequestMapping("showMenu")
    public Result showMenu(String username) {
        try {
            List<Menu> menuList = menuService.findMenuByUsername(username);
            return new Result(true, MessageConstant.GET_MENU_SUCCESS, menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MENU_FAIL);
        }
    }

    //查询所有菜单数据
    @RequestMapping("/findAll")
    public Result findAllMenu() {
        try {
            List<Menu> list = menuService.findAll();
            return new Result(true, "查询所有菜单信息成功", list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询所有菜单信息失败");
        }
    }

    //查询菜单角色关联
    @RequestMapping("/findMenuIdsByRoleId")
    public Result findMenuIdsByRoleId(Integer id) {
        try {
            List<Menu> list = menuService.findMenuIdsByRoleId(id);
            return new Result(true, "查询角色菜单关联id成功", list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询角色菜单关联id失败");

        }
    }
}
